import os
import random
import re
import eyed3

# 特别注意：本文件只能命令行运行

r_act = re.compile(r'.*(Act.*):')
r_song_name = re.compile(r'.*Act.*: (.*)')

r_carmen_act = re.compile(r'.*: (Act.*?) - .*')
r_carmen_song_name = re.compile(r'.*: Act.*? - (.*) \(')

r_chahua_act = re.compile(r'.*/ (Act.*?) - .*')
r_chahua_song_name = re.compile(r'.*/ Act.*? - (.*)$')

r_tld_act = re.compile(r'.*- (Act.*?). .*')
r_tld_song_name = re.compile(r'.*- Act.*?. (.*) \(')

r_fh_act = re.compile(r'.*(Act.*?),? S.*')
r_fh_song_name = re.compile(r'No\..*?(".*")')


def change_song_name():
    my_dir = '../static/song/'
    opr_list = os.listdir(my_dir)
    for opr_name in opr_list:
        song_list = os.listdir(my_dir + opr_name)
        for f in song_list:
            if re.match(r'.*\.mp3', f) is None:
                os.remove(my_dir + opr_name + '/' + f)
                continue
            mp3 = eyed3.load(my_dir + opr_name + '/' + f)
            name = ''.join(filter(str.isalpha, mp3.tag.album + mp3.tag.title))
            try:
                os.rename(my_dir + opr_name + '/' + f, my_dir + opr_name + '/' + name + '.mp3')
            except:
                print("出错了")
                print(my_dir + opr_name + '/' + f, my_dir + opr_name + '/' + name + '.mp3')
                continue


def handle_song():
    # 扫描目录
    my_dir = '../static/song/'
    opr_list = os.listdir(my_dir)
    out = open('song-script.txt', mode='w', encoding='utf-8')
    for opr_name in opr_list:
        song_list = os.listdir(my_dir + opr_name)
        for f in song_list:
            mp3 = eyed3.load(my_dir + opr_name + '/' + f)
            name = mp3.tag.title
            mp3_src = f
            score_src = ''
            # if opr_name == '卡门':
            #     try:
            #         act = r_carmen_act.findall(name)[0]
            #         name = r_carmen_song_name.findall(name)[0]
            #     except:
            #         if len(name) > 20:
            #             act = 'Act I'
            #             name = '"Près des remparts de Séville...Tais-toi"'
            #         else:
            #             act = 'Overture'
            #             name = 'Overture'
            # elif opr_name == '弄臣':
            #     act = r_act.findall(name)[0]
            #     name = r_song_name.findall(name)[0]
            # elif opr_name == '茶花女':
            #     try:
            #         act = r_chahua_act.findall(name)[0]
            #         name = r_chahua_song_name.findall(name)[0]
            #         if name == 'Prelude':
            #             name += ' ' + act
            #     except:
            #         act = 'Act 2'
            #         name = 'Prelude Act 2'
            # elif opr_name == '图兰朵':
            #     try:
            #         act = r_tld_act.findall(name)[0]
            #         name = r_tld_song_name.findall(name)[0]
            #     except:
            #         print("err")
            # else:
            if opr_name == '塞维利亚的理发师':
                if name[0] == 'A':
                    act = 'Act I'
                    name = 'Largo al factotum'
                else:
                    act = 'Act I'
                    name = '"Una voce poco fa...Io sono docile"'
            elif opr_name=='爱的甘醇':
                act = 'Act I'
            elif opr_name == '费加罗的婚礼':
                act = r_fh_act.findall(name)[0]
                name = r_fh_song_name.findall(name)[0]
            elif opr_name=='魔笛':
                act = r_chahua_act.findall(name)[0]
                name = r_chahua_song_name.findall(name)[0]
            else:
                continue
            res_str = "db.session.add(Song(name='''"
            res_str += name + "''', opr_name='''" + opr_name
            res_str += "''', mp3_src='" + mp3_src + "', score_src='"
            res_str += score_src + "', like_cnt=" + str(random.randint(1, 9999))
            res_str += ", is_classic=" + str(False)
            res_str += ", act='" + act + "'))\n"
            # print(res_str)
            out.write(res_str)
    out.write("db.session.commit()\n")
    out.close()


if __name__ == "__main__":
    change_song_name()
    handle_song()
